<template>
  <div class="matrial-main">
    <el-card
      shadow="never"
      class="margin-30"
      :body-style="{ padding: '20px' }"
    >
      <div slot="header">
        <el-upload
          ref="upload"
          :headers="token"
          action="http://leju.bufan.cloud/lejuAdmin/material/uploadFileOssSave"
          clear-files
          :before-upload="beforeAvatarUpload"
          :on-success="handleCoverImgSucc"
        >
          <el-button size="small" type="primary">点击上传素材</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过1.5MB</div>
        </el-upload>
      </div>
      <!-- card body -->
      <el-row :gutter="20">
        <el-col
          v-for="item in MaterList"
          :key="item.id"
          :offset="0"
          style="width: 20%;"
        >
          <el-card
            v-loading="loading"
            shadow="never"
            style="margin-bottom: 20px;"
            element-loading-text="拼命加载中"
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(0, 0, 0, 0.8)"
          >
            <div slot="header">
              <span class="title">创建时间:{{ item.createTime }}</span>
            </div>
            <el-image
              :src="item.ossUrl"
              :preview-src-list="previewSrcList"
              style="height: 250px; width: 100%; padding: 10px;"
            />
            <el-row :gutter="20">
              <el-col :span="4" :offset="18">
                <el-button type="danger" size="mini" style="margin-top: 10px;" @click="removeImgById(item.id)">删除</el-button>
              </el-col>
            </el-row>
          </el-card>
        </el-col>
      </el-row>

      <el-pagination
        style="margin-top: 30px; text-align: center;"
        :current-page="start"
        :page-sizes="[10, 20, 30]"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script>
import mix from '@/mixins'
import { findMaterialByPage, delMaterial } from '@/api/content/matrial'

export default {
  name: 'Material',
  mixins: [mix],
  data() {
    return {
      MaterList: [],
      loading: true
    }
  },
  computed: {
    previewSrcList() {
      return this.MaterList.map(item => item.ossUrl)
    }
  },

  mounted() {
    this.getMaterList()
  },

  methods: {
    // 上传之前的校验
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || 'image/png' || 'image/webp'
      const isLtSize = file.size / 1024 / 1024 < 1.5

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG/PNG/WEBP 格式!')
      }
      if (!isLtSize) {
        this.$message.error('上传头像图片大小不能超过 1.5MB!')
      }
      return isJPG && isLtSize
    },
    // 上传头像成功
    handleCoverImgSucc(response) {
      // this.$refs.coverImgUplaod.clearFiles()
      const { success, message } = response
      if (success) {
        this.$message.success('添加成功')
        this.getMaterList()
      } else {
        this.$message.error(message)
      }
      this.$refs.upload.clearFiles()
    },

    // 获取素材列表
    async getMaterList() {
      const res = await findMaterialByPage(this.start, this.limit)
      const { success, data, message } = res
      if (success) {
        this.MaterList = data.rows
        this.total = data.total
        this.loading = false
      } else {
        this.$message.error(message)
      }
    },
    // 分页
    handleSizeChange(val) {
      this.limit = val
      this.start = 1
      this.getMaterList()
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.start = val
      this.getMaterList()
      // console.log(`当前页: ${val}`);
    },

    removeImgById(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delMaterial(id)
        const { success, message } = res
        if (success) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getMaterList()
        } else {
          this.$message.error(message)
        }
      }).catch(() => {
        this.$message.error('已取消删除')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.matrial-main{

  .title{
    display: block;
    font-size: 14px;
  }
}
</style>
